/*
 *    This file is part of CeluMap.
 *
 *    CeluMap is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    CeluMap is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    Copyright 2006, 2007, 2008 Gonzalo Merayo
 *    gonzalo (at) celumap.com.ar
 *    www.celumap.com.ar
 *
 */

package editor;

import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;

import db.DataSource;

public class NewVersion extends JDialog {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JLabel labelVersion = null;

	private JLabel labelMap = null;

	private JTextField map = null;

	private JTextField version = null;

	private JLabel copyMap = null;

	private JLabel mapToCopy = null;

	private JCheckBox copyCheck = null;
	
	private ActionListener listener = null;

	private JButton Cancel = null;

	private JButton OK = null;

	private DataSource ds;

	private int oldMap;

	/**
	 * @param owner
	 */
	public NewVersion(Frame owner) {
		super(owner);
		initialize();
	}

	public NewVersion(Frame owner, ActionListener listener, String map, String version, DataSource ds, int oldMap) {
		super(owner);
		initialize();
		this.listener = listener;
		getMap().setText(map);
		this.ds = ds;
		this.oldMap = oldMap;
		getVersion().setText(version);
		this.mapToCopy.setText(map+" "+version);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 158);
		this.setTitle("Create New Version");
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
			gridBagConstraints31.gridx = 1;
			gridBagConstraints31.gridy = 4;
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.gridx = 0;
			gridBagConstraints21.gridy = 4;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 1;
			gridBagConstraints6.anchor = GridBagConstraints.WEST;
			gridBagConstraints6.insets = new Insets(0, 10, 0, 0);
			gridBagConstraints6.gridy = 3;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 1;
			gridBagConstraints5.anchor = GridBagConstraints.WEST;
			gridBagConstraints5.insets = new Insets(0, 5, 0, 0);
			gridBagConstraints5.gridy = 2;
			mapToCopy = new JLabel();
			mapToCopy.setText("none");
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.gridheight = 2;
			gridBagConstraints4.anchor = GridBagConstraints.EAST;
			gridBagConstraints4.gridy = 2;
			copyMap = new JLabel();
			copyMap.setText("copy map");
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.gridy = 1;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.weighty = 1.0;
			gridBagConstraints3.gridx = 1;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints2.gridy = 0;
			gridBagConstraints2.weightx = 1.0;
			gridBagConstraints2.weighty = 1.0;
			gridBagConstraints2.gridwidth = 1;
			gridBagConstraints2.gridheight = 1;
			gridBagConstraints2.gridx = 1;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.weighty = 0.0;
			gridBagConstraints1.gridwidth = 1;
			gridBagConstraints1.gridheight = 1;
			gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.insets = new Insets(0, 5, 0, 5);
			gridBagConstraints1.anchor = GridBagConstraints.EAST;
			gridBagConstraints1.weightx = 0.0;
			labelMap = new JLabel();
			labelMap.setText("map");
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.insets = new Insets(0, 5, 0, 5);
			gridBagConstraints.gridy = 1;
			gridBagConstraints.ipadx = 0;
			gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints.weightx = 0.0;
			gridBagConstraints.weighty = 0.0;
			gridBagConstraints.gridwidth = 1;
			gridBagConstraints.gridheight = 1;
			gridBagConstraints.anchor = GridBagConstraints.EAST;
			gridBagConstraints.gridx = 0;
			labelVersion = new JLabel();
			labelVersion.setText("version");
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
			jContentPane.add(labelVersion, gridBagConstraints);
			jContentPane.add(labelMap, gridBagConstraints1);
			jContentPane.add(getMap(), gridBagConstraints2);
			jContentPane.add(getVersion(), gridBagConstraints3);
			jContentPane.add(copyMap, gridBagConstraints4);
			jContentPane.add(mapToCopy, gridBagConstraints5);
			jContentPane.add(getCopyCheck(), gridBagConstraints6);
			jContentPane.add(getCancel(), gridBagConstraints21);
			jContentPane.add(getOK(), gridBagConstraints31);
		}
		return jContentPane;
	}

	/**
	 * This method initializes map	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getMap() {
		if (map == null) {
			map = new JTextField();
		}
		return map;
	}

	/**
	 * This method initializes version	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getVersion() {
		if (version == null) {
			version = new JTextField();
		}
		return version;
	}

	/**
	 * This method initializes copyCheck	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getCopyCheck() {
		if (copyCheck == null) {
			copyCheck = new JCheckBox();
		}
		return copyCheck;
	}

	/**
	 * This method initializes Cancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getCancel() {
		if (Cancel == null) {
			Cancel = new JButton();
			Cancel.setText("Cancel");
			Cancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dispose();
				}
			});
		}
		return Cancel;
	}

	/**
	 * This method initializes OK	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getOK() {
		if (OK == null) {
			OK = new JButton();
			OK.setText("OK");
			OK.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					int newMap = ds.createVersion(map.getText(), version.getText());
					if(copyCheck.isSelected()){
						ds.copyMap(oldMap, newMap);
					}
					if(listener!=null)
						listener.actionPerformed(null);
				}
			});
		}
		return OK;
	}

}  //  @jve:decl-index=0:visual-constraint="14,0"
